package com.example.demo.service;

import com.example.demo.event.UserRegisterEvent;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author LIN
 * @date 2022-03-15
 */
@Service
public class UserRegisterService implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Qualifier(value = "demo1")
    @Autowired
    private Demo demo1;

    @Qualifier(value = "demo2")
    @Autowired
    private Demo demo2;

    @Resource
    private TxTestService txTestService;

    @Bean(name = "demo2")
    public  Demo getDemo(){
        return new Demo2();
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public boolean register(User user){
        System.out.println("[service]用户["+user+"]注册成功！");
        // 消息发布
        publishUserRegisterEvent(user);
        return true;
    }

    public void publishUserRegisterEvent(User user) {
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this, user));
    }

//    @Transactional(propagation = Propagation.MANDATORY)
    public void t1(){
       txTestService.t2();
    }

    public void qualifyTest() {
        demo1.print();
        demo2.print();
    }
}
